import {getAreaConfig, getIndustryConfig} from '@/utils/filterConfig';
import {
  getBondType,
  getCaliberType,
  getCompanyType,
  getFinancialType, getVarietyType, getGreenCertificationResult
} from './../../../apis';
import {ComponentType} from 'vc';
import {useRequest} from 'vue-request';

export function useScreen(companyId) {
  const PAGE_SIZE = 15;

  const queryData = reactive<any>({
    pageNum: 1,
    pageSize: PAGE_SIZE,
    searchTxt: '',
    sortRule: '',
    provinceCode: '',
    cityCode: '',
    countryCode: '',
    sort: 'desc',
  });

  // 地区
  const areaData = getAreaConfig();
  areaData.value.submitFn = (val) => {
    const {lv1, lv2, lv3} = val;

    queryData.provinceCode = lv1.join();
    queryData.cityCode = lv2.join();
    queryData.countryCode = lv3.join();
  };

  // 认证结果
  const {data: certificateResultData} = useRequest(getGreenCertificationResult, {
    defaultParams: [{id: companyId}]
  });
  const certificateResultTypeData = {
    title: '认证结果',
    component: ComponentType.MULTIPLE,
    options: certificateResultData,
    required: false,
    submitFn: (data) => {
      queryData.certificationResult = data;
    }
  };

  // 绿债口径分类
  const {data: caliberData} = useRequest(getCaliberType, {});
  const caliberTypeData = {
    title: '绿债口径分类',
    component: ComponentType.SINGLE,
    options: caliberData,
    required: false,
    submitFn: (data) => {
      queryData.greenBondCaliber = data;
    }
  };

  // 绿债品种分类
  const {data: varietyData} = useRequest(getVarietyType, {});
  const varietyTypeData = {
    title: '绿债品种分类',
    component: ComponentType.SINGLE,
    options: varietyData,
    required: false,
    submitFn: (data) => {
      queryData.greenBondVariety = data;
    }
  };


  // 债券类型
  const {data} = useRequest(getBondType, {});
  const classTypeData = {
    title: '债券类型',
    component: ComponentType.MULTIPLE,
    options: data,
    submitFn: (data) => {
      queryData.bondType = data;
    }
  };


  // 发行日期
  const dateData = {
    title: '发行日期',
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.issuingDateStart = data[0];
      queryData.issuingDateEnd = data[1];
    }
  };
  // 认证日期
  const dateCertificateData = {
    title: '认证日期',
    component: ComponentType.DATE,
    type: 'daterange',
    submitFn: (data) => {
      queryData.certificationDateStart = data[0];
      queryData.certificationDateEnd = data[1];
    }
  };

  const bondTypeDataMoreOption = [
    {
      name: '绿债口径分类',
      code: 'greenBondCaliber',
      data: caliberData,
      isNoLimit: true
    },
    {
      name: '绿债品种分类',
      code: 'greenBondVariety',
      data: varietyData,
      isNoLimit: true
    },
    {
      name: '债券类型',
      code: 'bondType',
      data: data,
      multiple: true,
      isNoLimit: true
    },
  ];
  // 更多
  const moreBondTypeFilter = ref({
    title: '债券分类',
    component: 'tiled-select',
    options: bondTypeDataMoreOption,
    submitFn: (data) => {
      Object.assign(queryData, data);
    }
  });

  // 是否到期
  const maturityTypeData = {
    title: '是否到期',
    component: ComponentType.SINGLE,
    options: [
      {name: '是', code: 2},
      {name: '否', code: 1}
    ],
    required: false,
    submitFn: (data) => {
      queryData.maturityType = data;
    }
  };

  const screen = ref([moreBondTypeFilter.value, dateData, dateCertificateData, certificateResultTypeData, maturityTypeData
  ]);

  return {
    PAGE_SIZE,
    screen,
    queryData
  };
}
